package com.hncc.dms.service.impl;

import com.hncc.dms.mapper.DmsDiseMapper;
import com.hncc.dms.mapper.DmsDrugMapper;
import com.hncc.dms.mapper.DmsNonDrugMapper;
import com.hncc.dms.service.ISmsFrequentUsedService;
import com.hncc.sms.domain.SmsFrequentUsed;
import com.hncc.sms.domain.SmsFrequentUsedVo;
import com.hncc.sms.mapper.SmsFrequentUsedMapper;
import com.hncc.sms.mapper.SysUserManagerMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 常用项管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-29
 */
@Service
public class SmsFrequentUsedServiceImpl implements ISmsFrequentUsedService
{
    @Resource
    private SmsFrequentUsedMapper smsFrequentUsedMapper;

    @Resource
    private DmsDrugMapper dmsDrugMapper;

    @Resource
    private DmsNonDrugMapper dmsNonDrugMapper;

    @Resource
    private DmsDiseMapper dmsDiseMapper;

    @Resource
    private SysUserManagerMapper sysUserManagerMapper;

    /**
     * 查询常用项管理
     *
     * @return 常用项管理
     */
    @Override
    public List<?> selectSmsFrequentUsedById(SmsFrequentUsedVo smsFrequentUsedVo)
    {
        //获取当前所属人Id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //根据所属人Id查询该所属人是否有常用项
        SmsFrequentUsed smsFrequentUsed = smsFrequentUsedMapper.selectSmsFrequentUsedById(null,userId);
        if(userId==null){
            smsFrequentUsed.setStaffId(userId);
            //新增一个该所属人的常用项
            smsFrequentUsedMapper.insertSmsFrequentUsed(smsFrequentUsed);
            return null;
        }else{
            smsFrequentUsed.setType(smsFrequentUsedVo.getType());
            System.out.println("getType->"+smsFrequentUsed.getType());
            System.out.println("toString->"+smsFrequentUsed.toString());
            return frequentTypeId(smsFrequentUsed);
        }
    }

    public List<?> frequentTypeId(SmsFrequentUsed smsFrequentUsed){
        if(smsFrequentUsed.getType() == 1){
            return dmsNonDrugMapper.selectDmsNonDrugByIds(smsFrequentUsed.getCheckIdList().split(","), null);
        }else if(smsFrequentUsed.getType() == 2){
            return dmsNonDrugMapper.selectDmsNonDrugByIds(smsFrequentUsed.getTestIdList().split(","), null);
        }else if(smsFrequentUsed.getType() == 3){
            return dmsNonDrugMapper.selectDmsNonDrugByIds(smsFrequentUsed.getDispositionIdList().split(","), null);
        }else if(smsFrequentUsed.getType() == 4){
            return dmsDiseMapper.selectDmsDiseByIds(smsFrequentUsed.getMedicineDiseIdList().split(","));
        }else if(smsFrequentUsed.getType() == 5 || smsFrequentUsed.getType() == 6){
            return dmsDrugMapper.selectDmsDrugByTypeIdOrId(smsFrequentUsed.getType(), smsFrequentUsed.getDrugIdList().split(","));
        }
        return null;
    }

    /**
     * 查询常用项管理列表
     * 
     * @param smsFrequentUsed 常用项管理
     * @return 常用项管理
     */
    @Override
    public List<SmsFrequentUsed> selectSmsFrequentUsedList(SmsFrequentUsed smsFrequentUsed)
    {
        return smsFrequentUsedMapper.selectSmsFrequentUsedList(smsFrequentUsed);
    }

    /**
     * 新增常用项管理
     * 
     * @param smsFrequentUsed 常用项管理
     * @return 结果
     */
    @Override
    public int insertSmsFrequentUsed(SmsFrequentUsed smsFrequentUsed)
    {
        return smsFrequentUsedMapper.insertSmsFrequentUsed(smsFrequentUsed);
    }

    /**
     * 修改常用项管理
     * @param type 当前常用项类型
     * @param id 新增id
     * @return
     */
    @Override
    public int updateSmsFrequentUsed(Integer type, Integer id,String addOrUpd)
    {
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        SmsFrequentUsed smsFrequentUsed = smsFrequentUsedMapper.selectSmsFrequentUsedById(null,userId);
        String str = getFrequentUsed(type,smsFrequentUsed);
        List<String> array = Arrays.asList(str.split(","));
        List<String> arrays = new ArrayList<>(array);
        if("add".equals(addOrUpd)){
            arrays.add(id.toString());
        }else{
            for (int i = 0; i < arrays.size(); i++){
                if(arrays.get(i).equals(id.toString())){
                    arrays.remove(arrays.get(i));
                }
            }
        }
        str = String.join(",",arrays);
        smsFrequentUsed = setFrequentUsed(type,str,smsFrequentUsed);
        return smsFrequentUsedMapper.updateSmsFrequentUsed(smsFrequentUsed);
    }

    /**
     * 返回响应常用项的字符串
     * @param type
     * @param smsFrequentUsed
     * @return
     */
    public String getFrequentUsed(Integer type,SmsFrequentUsed smsFrequentUsed){
        if(type == 1){
            return smsFrequentUsed.getCheckIdList();
        }else if(type == 2){
            return smsFrequentUsed.getTestIdList();
        }else if(type == 3){
            return smsFrequentUsed.getDispositionIdList();
        }else if(type == 4){
            return smsFrequentUsed.getMedicineDiseIdList();
        }else if(type == 5 || type == 6){
            return smsFrequentUsed.getDrugIdList();
        }
        return null;
    }

    /**
     * 改变当前常用项的字段
     * @param type
     * @param str
     * @param smsFrequentUsed
     * @return
     */
    public SmsFrequentUsed setFrequentUsed(Integer type, String str,SmsFrequentUsed smsFrequentUsed){
        if(type == 1){
            smsFrequentUsed.setCheckIdList(str);
        }else if(type == 2){
            smsFrequentUsed.setTestIdList(str);
        }else if(type == 3){
            smsFrequentUsed.setDispositionIdList(str);
        }else if(type == 4){
            smsFrequentUsed.setMedicineDiseIdList(str);
        }else if(type == 5 || type == 6){
            smsFrequentUsed.setDrugIdList(str);
        }
        return smsFrequentUsed;
    }

    /**
     * 批量删除常用项管理
     * 
     * @param ids 需要删除的常用项管理ID
     * @return 结果
     */
    @Override
    public int deleteSmsFrequentUsedByIds(Long[] ids)
    {
        return smsFrequentUsedMapper.deleteSmsFrequentUsedByIds(ids);
    }

    /**
     * 删除常用项管理信息
     * 
     * @param id 常用项管理ID
     * @return 结果
     */
    @Override
    public int deleteSmsFrequentUsedById(Long id)
    {
        return smsFrequentUsedMapper.deleteSmsFrequentUsedById(id);
    }

    @Override
    public SmsFrequentUsed getDrugIdListByUserId() {
        //获取当前所属人Id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //根据所属人Id查询该所属人常用项
        return smsFrequentUsedMapper.selectSmsFrequentUsedById(null,userId);
    }

    @Override
    public String selectDiseIds() {
        return smsFrequentUsedMapper.selectDiseIds();
    }

    @Override
    public String selectNonDrugIds(Integer type) {
        //获取当前所属人Id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        return smsFrequentUsedMapper.selectNonDrugIds(type,userId);
    }
}
